package com.hldsmart.smartda.adapter.home;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.hldsmart.smartda.R;
import com.hldsmart.smartda.base.BaseObjectListAdapter;
import com.hldsmart.smartda.model.entity.Entity;
import com.hldsmart.smartda.model.entity.ShowEntity;
import com.hldsmart.smartda.utils.MyListView;

import java.util.ArrayList;
import java.util.List;


/**
 * 子listview适配器
 * @author mmsx
 *
 */
public class ParentAdapt extends BaseObjectListAdapter {

	private ArrayList<ShowEntity> mChildList;
	public static int mParentItem = -1;
	public static boolean mbShowChild = false;
	
	public ParentAdapt(Context context, List<? extends Entity> datas) {
		super(context, datas);
		initData();
	}
	
	private void initData() {
		mChildList = new ArrayList<ShowEntity>();
		mChildList.clear();
		ShowEntity tempEntity = new ShowEntity("服装店回复：", "首先应该选择有个性一点的服装又要考虑保暖问题，这件中长款的卫衣是不错的选择，可以到我店购买哦~"," ");
		ShowEntity tempEntity1 = new ShowEntity("鲜花店回复：", "您好我们店最近新推出的情人节专属套餐，99朵红玫瑰，大气礼盒包装代写贺卡福语特别适合情人节送女友"," ");
        mChildList.add(tempEntity);
        mChildList.add(tempEntity1);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.activity_main_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_user_photo = (ImageView) convertView
                    .findViewById(R.id.iv_user_head);
            viewHolder.tv_user_name = (TextView) convertView
                    .findViewById(R.id.character_item_name);
            viewHolder.tv_user_comment = (TextView) convertView
                    .findViewById(R.id.character_item_story);
            viewHolder.character_item_time = (TextView) convertView
                    .findViewById(R.id.character_item_time);
            viewHolder.listViewItem = (ListView)convertView.findViewById(R.id.listView_child);
			convertView.setTag(viewHolder);
		}else {
            viewHolder = (ViewHolder) convertView.getTag();
		}
		
		ShowEntity tempEntity = (ShowEntity) mDatas.get(position);
        viewHolder.iv_user_photo.setImageResource(R.drawable.iv_photo);
        viewHolder.tv_user_name.setText(tempEntity.getName());
        viewHolder.tv_user_comment.setText(tempEntity.getStory());
        viewHolder.character_item_time.setText(tempEntity.getTime());
		
		//����Ǹ������Ǹ�������Ѿ��������ջ���listview
		if (mParentItem == position && mbShowChild) {
			//��listviewʵ������������ݵ�
			ChildAdapt tempAdapt = new ChildAdapt(mContext, mChildList);
            viewHolder.listViewItem.setAdapter((ListAdapter) tempAdapt);
            viewHolder.listViewItem.setVisibility(View.VISIBLE);
			
			//��listview�ĵ������
            viewHolder.listViewItem.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
										int position, long id) {
					Toast.makeText(mContext, "Child Listview" + String.valueOf(position), Toast.LENGTH_SHORT).show();
					
				}
			});
		}else {
            viewHolder.listViewItem.setVisibility(View.GONE);
		}
		
//		vHolder.buttonStake.setOnClickListener(new ParentButtonLisener());
//		//��ס���Ǹ�button�¼�
//		vHolder.buttonStake.setTag(position);
	    
		return convertView;
	}
	
	class ViewHolder{
        private ImageView iv_user_photo; // 头像
        private TextView tv_user_name; // 名称
        private TextView tv_user_comment; // 问题
        private TextView character_item_time; //时间
//		TextView textViewTitle;
//		TextView textViewContent;
		ListView listViewItem;
	}
	
//	//��listview��button�ļ���
//	private class ParentButtonLisener implements OnClickListener {
//		@Override
//		public void onClick(View v) {
//			Integer nPosition = (Integer) (v.getTag());
//			Toast.makeText(mContext, "��ʾ" + String.valueOf(nPosition.intValue()), Toast.LENGTH_SHORT).show();
//		}
//	}
}
